package c.x.jy.common.utils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;

/**
 * Created by wugr on 2016/4/27 0027.
 */
@SuppressWarnings("all")
public class SysUtils {

    private final static char[] DIGITS64 = "0123456789abcdefghijklmnopqrstuv".toCharArray();


    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };


  

    private static boolean startWithRoots(String dir,File[] roots){
        for (int i =0;i<roots.length;i++){
            File file = roots[i];
            if(dir.startsWith(file.getPath())){
                return true;
            }
        }
        return false;
    }

    public static String printStackTraceToString(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }

    public static String generateCode(){
        UUID u = UUID.randomUUID();
        String x = u.toString().replaceAll("-","");
        return x;
    }

    public static String generateShortCode() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 10; i++) {
            String str = uuid.substring(i * 3, i * 3 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public static boolean judgeIsMoblie(HttpServletRequest request) {
        boolean isMoblie = false;
        String[] mobileAgents = { "iphone", "android","ipad", "phone", "mobile", "wap", "netfront", "java", "opera mobi",
                "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod",
                "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
                "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
                "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
                "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
                "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320",
                "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
                "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs",
                "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi",
                "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port",
                "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem",
                "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v",
                "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-",
                "Googlebot-Mobile" };
        if (request.getHeader("User-Agent") != null) {
            String agent=request.getHeader("User-Agent");
            for (String mobileAgent : mobileAgents) {
                if (agent.toLowerCase().indexOf(mobileAgent) >= 0&&agent.toLowerCase().indexOf("windows nt")<=0 &&agent.toLowerCase().indexOf("macintosh")<=0) {
                    isMoblie = true;
                    break;
                }
            }
        }
        return isMoblie;
    }

    public static void main(String[] args) throws InterruptedException {
        HashSet set = new HashSet();
        for(int i=0;i<1000000;i++) {
            String r = generateShortCode();
            //System.out.println(r);
            if(i==100000){
                System.out.println(r);
              //  set.add(r);
            }
             if (set.contains(r)) {
                 System.out.println(r + "重复");
            }
           // Thread.sleep(100);
          //   System.out.println(r);
            set.add(r);
        }
        System.out.println(set.size());
        
    }

	 

	public static <T> List<T> StrAsList(List<String> asList, Class<T> c)   {
		List<T> list=new ArrayList<>();
		for (String str : asList) {
		//	System.out.println(str);  
			 try {
				 Class cl = Class.forName(c.getName());
					Method method = cl.getMethod("valueOf", String.class);
					Object o = method.invoke(null, str);
					list.add(  (T)  o );
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	//	System.out.println(list);
		return list;
	}
	
	public static String getFormatKey(Object... o ) {
		 
			return  JSON.toJSONString(o);
	 
	}

	public static String ListToStr(Collection<Long> lids) {
		String ids=null;
		for (Long i : lids) {
			ids = ((ids == null) ? (i + "") : (ids + "," + i));
		}
		return ids==null?"":ids;
	}

    public static Long generateSnowflake(Integer workId,Integer dataId) {
     return   IdUtil.getSnowflake( workId, dataId).nextId();
    }
}
